#pragma once
//MySQL 版本
#include "../comm/util.hpp"
#include "../comm/log.hpp"
#include <mysql/mysql.h>

#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <fstream>
#include <cstdlib>
#include <cassert>


// 根据题目list文件，加载所有的题目信息到内存中
// model: 主要用来和数据进行交互，对外提供访问数据的接口

namespace ns_model
{
    using namespace std;
    using namespace ns_log;
    using namespace ns_util;

    struct Question
    {
        std::string number; //题目编号，唯一
        std::string title;  //题目的标题
        std::string star;   //难度: 简单 中等 困难
        std::string desc;   //题目的描述
        std::string header; //题目预设给用户在线编辑器的代码
        std::string tail;   //题目的测试用例，需要和header拼接，形成完整代码
        int cpu_limit;      //题目的时间要求(S)
        int mem_limit;      //题目的空间要去(KB)
    };

    const std::string oj_questions = "oj_questions";
    const std::string host = "127.0.0.1";
    const std::string usr = "oj_client";
    const std::string passwd = "YJD010918madeinchina...";
    const std::string db = "oj";
    const int port = 8088;

    class Model
    {
    public:
        Model()
        {}
        bool QueryMysql(const string &sql, vector<Question> *out)
        {
            // 1.创建mysql句柄
            MYSQL *my = mysql_init(nullptr);
            // 2.链接数据库
            if (nullptr == mysql_real_connect(my, host.c_str(), usr.c_str(), passwd.c_str(), db.c_str(), port, nullptr, 0))
            {
                LOG(FATAL) << "链接数据库失败!" << endl;
                return false;
            }
            // 3.设置链接编码格式
            mysql_set_character_set(my, "utf8");
            LOG(INFO) << "链接数据库成功!" << endl;

            // 4.访问数据库
            if (0 != mysql_query(my, sql.c_str()))
            {
                LOG(WARRING) << sql << "execute error!" << endl;
                return false;
            }
            // 5.从MYSQL句柄中获取查询结果
            MYSQL_RES *res = mysql_store_result(my);

            // 6.解析结果，读取记录
            int rows = mysql_num_rows(res);
            int fileds = mysql_num_fields(res);
            Question q;
            for (int i = 0; i < rows; i++)
            {
                MYSQL_ROW line = mysql_fetch_row(res);
                q.number = line[0];
                q.title = line[1];
                q.star = line[2];
                q.desc = line[3];
                q.header = line[4];
                q.tail = line[5];
                q.cpu_limit = atoi(line[6]);
                q.mem_limit = atoi(line[7]);
                out->push_back(q);
            }
            //7.关闭链接
            mysql_close(my);
            return true;
        }
         
        // 获取所有的题目的题目信息
        bool GetAllQuestions(vector<Question> *out)
        {
            string sql = "select *from ";
            sql += oj_questions;
            return QueryMysql(sql, out);
        }
        // 通过题目编号获取这个题目的细节信息
        bool GetOneQuestion(const string &number, Question *q)
        {
            string sql = "select * from ";
            sql += oj_questions;
            sql += " where number= ";
            sql += number;
            vector<Question> result;
            if (QueryMysql(sql, &result))
            {
                if (result.size() == 1)
                {
                    *q = result[0];
                    return true;
                }
            }
            return false;
        }
        ~Model()
        {}
    };
} 
